#include <QtGui/QBoxLayout>
#include <QtGui/QApplication>
#include <QtCore/QDebug>

#include "RecoverWidget.h"

RecoverWidget::RecoverWidget(QWidget *parent) : QWidget(parent)
{
    m_recoverButton = new QPushButton;
    m_infoLabel = new QLabel;

    initUI();

    connect(m_recoverButton, SIGNAL(clicked()), this, SLOT(RecoverButtonClicked()));
    connect(m_quitButton, SIGNAL(clicked()), this, SLOT(quitButtonClicked()));
}

void RecoverWidget::initUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;

    m_infoLabel = new QLabel(tr("recover to init state"));
    m_quitButton = new QPushButton(tr("quit"));
    m_recoverButton = new QPushButton(tr("recover"));
    m_recoverButton->setFixedWidth(100);
    m_quitButton->setFixedWidth(100);
    m_infoLabel->setWordWrap(true);

    buttonLayout->addSpacing(200);
    buttonLayout->addWidget(m_recoverButton);
    //buttonLayout->addWidget(m_quitButton);

    mainLayout->addWidget(m_infoLabel);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);
}

void RecoverWidget::RecoverButtonClicked()
{
    if(clean())
    {
        emit nextPage();
    }
    else
    {
        m_infoLabel->setText(tr("error ocurred, please press done button again"));
        m_recoverButton->setText(tr("recover againe"));
    }
}

bool RecoverWidget::clean()
{
    QString recoverCmd = QString("gksu ./recover/recover.sh");

    if(system(recoverCmd.toUtf8()) == 0)
    {
        return true;
    }

    return false;
}

void RecoverWidget::quitButtonClicked()
{
    qApp->quit();
}
